package com.example.sms_emoticon_library;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class FragmentEmoticons extends Fragment {

	Button btnTest;
	EmoticonsOnCLick onclick;
	private GridView gridViewEmotions; // add for GridView
    private int []iconID; // Select array of emotion icons id.
    private EmoticonsAdapter emAdapter;
    private LinearLayout layoutEmoticons;
    private SmileyParser smileyPar;
	
	
	
		
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		if(activity instanceof EmoticonsOnCLick){
			onclick = (EmoticonsOnCLick)  activity;
		}else {
			throw new ClassCastException(activity.toString() + "must implemenet FragmentEmotion.OnCLick"
					);
		}
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SmileyParser.init(getActivity());
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(container==null){
			return null;
		}
		View view= (LinearLayout) inflater.inflate(R.layout.fragment_emoticons_layout, container, false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		smileyPar=SmileyParser.getInstance();
		//smileyPar= SmileyParser.init(getActivity());
		gridViewEmotions= (GridView) getActivity().findViewById(R.id.gv_Emoticons);
		layoutEmoticons= (LinearLayout) getActivity().findViewById(R.id.layoutEm);
		iconID= com.example.sms_emoticon_library.SmileyParser.DEFAULT_SMILEY_RES_IDS;
		emAdapter= new EmoticonsAdapter(getActivity().getApplicationContext(),iconID);
		gridViewEmotions.setAdapter(emAdapter);
		gridViewEmotions.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				myChar(arg2);
				
			}
		});
		
		
		btnTest =  (Button) getActivity().findViewById(R.id.btn_Backspace);
		btnTest.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				updateComment();
			}		
		});
		
	}
	
	public interface  EmoticonsOnCLick{
		public void buttonCLick(String link); 
		public void gvClick(String item);
		};

	public void updateComment() {
		String newString= "this is  a OnLick inteface";
		onclick.buttonCLick(newString);
		
	}
	
	public void myChar(int index){
		
		smileyPar=SmileyParser.getInstance();
		String mText="";

		int position = index;
		
		if(smileyPar!=null){
		mText = smileyPar.MyChar(position);
		}else{
			mText="null";
		}
		onclick.gvClick(mText);
	}

	
	
	
	
}
